//
//  BDMPageControl.m
//  iJobs
//
//  Created by liyingpeng on 15/12/24.
//  Copyright © 2015年 liyingpeng. All rights reserved.
//

#import "BDMPageControl.h"

@implementation BDMPageControl

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self baseInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self baseInit];
    }
    return self;
}

- (void)baseInit {
    [self setBackgroundColor:[UIColor clearColor]];
    _pageControlStyle = BDMPageControlStyleDefault;
    _showPageCount = 1;
    _currentPage = -1;
}

- (void)drawRect:(CGRect)rect {
    UIColor *normalColor = self.pageIndicatorTintColor ? self.pageIndicatorTintColor : [UIColor whiteColor];
    UIColor *selectedColor = self.currentPageIndicatorTintColor ? self.currentPageIndicatorTintColor : [UIColor blackColor];
    
    if (self.hidesForSinglePage && self.numberOfPages == 1) {
        return;
    }
    
    CGContextRef myContext = UIGraphicsGetCurrentContext();
    for (int i = 0; i < self.numberOfPages; i++) {
        float dotHeight = self.currentPageIndicatorForeImage ? self.currentPageIndicatorForeImage.size.height : rect.size.height;
        CGRect dotRect;
        if (self.numberOfPages > 1) {
            float dotGap = (self.bounds.size.width - (self.numberOfPages * dotHeight)) / (self.numberOfPages - 1);
            float dotLeft = (dotHeight + dotGap) * i;
            float dotTop = (self.bounds.size.height - dotHeight) / 2.;
            dotRect = CGRectMake(dotLeft, dotTop, dotHeight, dotHeight);
        }
        else {
            dotRect = CGRectMake(0, 0, dotHeight, dotHeight);
        }
        
        if (self.pageControlStyle == BDMPageControlStyleDefault) {
            if ([self isDotSelected:i]) {
                CGContextSetFillColorWithColor(myContext, [selectedColor CGColor]);
                CGContextFillEllipseInRect(myContext, dotRect);
            }
            else {
                CGContextSetFillColorWithColor(myContext, [normalColor CGColor]);
                CGContextFillEllipseInRect(myContext, dotRect);
            }
        }
        else if (self.pageControlStyle == BDMPageControlStyleForeImage) {
            if (self.pageIndicatorForeImage && self.currentPageIndicatorForeImage) {
                if ([self isDotSelected:i]) {
                    [self.currentPageIndicatorForeImage drawInRect:dotRect];
                }
                else {
                    [self.pageIndicatorForeImage drawInRect:dotRect];
                }
            }
        }
    }
}

- (BOOL)isDotSelected:(NSInteger)index {
    if (self.currentPage < 0) {
        return NO;
    }
    if (self.currentPage + self.showPageCount <= self.numberOfPages) {
        for (NSInteger i = self.currentPage; i < self.currentPage + self.showPageCount; i++) {
            if (index == i) {
                return YES;
            }
        }
    }
    else {
        for (NSInteger i = self.currentPage; i < self.numberOfPages; i++) {
            if (index == i) {
                return YES;
            }
        }
        for (NSInteger i  = 0; i < self.currentPage + self.showPageCount - self.numberOfPages; i++) {
            if (index == i) {
                return YES;
            }
        }
    }
    return NO;
}

- (void)setPageControlStyle:(BDMPageControlStyle)pageControlStyle {
    _pageControlStyle = pageControlStyle;
    [self setNeedsDisplay];
}

- (void)setCurrentPage:(NSInteger)currentPage {
    _currentPage = currentPage;
    [self setNeedsDisplay];
}

- (void)setNumberOfPages:(NSInteger)numberOfPages {
    _numberOfPages = numberOfPages;
    [self setNeedsDisplay];
}

- (void)setShowPageCount:(NSInteger)showPageCount {
    _showPageCount = showPageCount;
    [self setNeedsDisplay];
}

@end
